package com.jec.module.sysconfig.service.convertor;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jec.module.sysconfig.dao.RelayBusinessRepository;
import com.jec.module.sysconfig.entity.RelayBusiness;
import com.jec.module.sysmonitor.dao.NetUnitRepository;
import com.jec.utils.Response;

/**
 * Created by jeremyliu on 9/26/16.
 */
@Service
public class RelayBusinessConvertor implements Convertor<RelayBusiness> {

    @Autowired
    private NetUnitRepository netUnitRepository;

    @Autowired
    private RelayBusinessRepository relayBusinessRepository;

    @Override
    @Transactional(readOnly = true)
    public Response process(RelayBusiness relayBusiness) {
        Response response = Response.Builder().status(Response.STATUS_PARAM_ERROR);

        if(netUnitRepository.findOne(relayBusiness.getNetunit()) == null)
            return response.message("网元不存在");

        if(relayBusinessRepository.existsByNetunitAndIdNot(
        		relayBusiness.getNetunit(),
        		relayBusiness.getId()))
            return response.message("该网元的槽位端口的中继已配置");

        return response.status(Response.STATUS_SUCCESS);
    }

    @Override
    public Response checkBeforeRemove(int id) {
        return null;
    }
}
